<?php
/**
 * Project:     Rebel PHP Framework
 * File:        Ldap.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.rebelphpframework.org
 * @copyright 2007 Emanuele Colonnelli
 * @author Emanuele Colonnelli <emanuele.colonnelli at gmail dot com>
 * @package kernel
 * @version 1.0
 */
if(define(__FILE__,true)) {
	
	/**
	 * Abstract class used to use native PHP ldap functions in a OO fashion.
	 * __get public method allow you to invoke ( as example ) ldap_search with this
	 * syntax $ldapInstance->search().
	 * Exception handling, not present into native php function was implemented... happy coding!
	 *
	 * @author Emanuele Colonnelli <emanuele.colonnelli@acotel.com>
	 * @version 1.0
	 * @uses Rebel_Config_XMLConfig
	 * 
	 */
	abstract class Rebel_Ldap_Ldap {
		
		const LDAP_PROTOCOL_VERSION = 3;
		
		private $host,
				$bind_dn,
				$port,
				$password,
				$conn;
		
		protected function connect($host,$bind_dn,$password,$port=null) {

			if(!$this->conn = ldap_connect($host,$port)) {
				throw new Exception(ldap_error($this->conn),ldap_errno($this->conn));
			} else {
				
				$this->host = $host;
				$this->bind_dn = $bind_dn;
				$this->port = $port;
				$this->password = $password;

				ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, self::LDAP_PROTOCOL_VERSION);
				ldap_bind($this->conn,$this->bind_dn,$this->password);
				
				return $this->conn;
			}

		}//:-
		
		public function getConn() {
			return $this->conn;
		}//:-
		
		public function __call($function,$args) {
			
			if(!$res = call_user_func_array('ldap_'.$function,$args)) {
				throw new Exception(ldap_error($this->conn),ldap_errno($this->conn));
			}
			return $res;
		}//:-
	}///:-
}
?>